import java.awt.* ;
import java.awt.List;
import java.awt.event.* ;
import java.util.*;
import javax.swing.* ;


public class LogInOut extends JPanel{

	private JLabel jLabel1;
	private JLabel userLabel;
	private JLabel pwdLabel;
	private JTextField userNameTextField;
	private JPasswordField pwdTextField;
	private JButton logButton;
	private JButton regButton;
	private DomainLogic dL;
	private JPanel row;
	private JPanel row2;

	public LogInOut(DomainLogic domainLogic){
		dL = domainLogic;
		row = new JPanel();
		row.setLayout(new GridLayout(2,2));
		row2 = new JPanel();
		row2.setLayout(new BoxLayout(row2, BoxLayout.X_AXIS));
		
		this.setLayout(new GridLayout(0,1));
		//this.setPreferredSize(new Dimension(25,50));
		//this.setMinimumSize(new Dimension(100,75));
		this.setMaximumSize(new Dimension(100,75));
		jLabel1 = new JLabel("Logon");
		this.add(jLabel1);	
		
		userLabel = new JLabel("Username:");
		userNameTextField = new JTextField();
		userNameTextField.setMaximumSize(new Dimension(200,20));
		
		pwdLabel = new JLabel("Password:");
		pwdTextField = new JPasswordField();
		pwdTextField.setMaximumSize(new Dimension(200,20));
		
		row.add(userLabel);
		row.add(pwdLabel);
		row.add(userNameTextField);
		row.add(pwdTextField);
		this.add(row);
		
		logButton = new JButton("Sign in");
		logButton.setMaximumSize(new Dimension(100,20));
		row2.add(logButton);
				
		regButton = new JButton("Register");
		regButton.setMaximumSize(new Dimension(100,20));
		row2.add(regButton);
		
		this.add(row2);

	}
	
	// Proof
	// Precondition: User is not logged on.
	// If the username and password pair are not found in the database,
	// then the user is not logged on.
	// Postconditon: User is either logged on or off. 
	// If username and password are not true, then nothing changes 
	// and 
	public boolean signIn() {
		String username = userNameTextField.getText();
		String password = pwdTextField.getText();
		if (dL.logOn(username,password)) {
			logButton.setText("Sign out");
			jLabel1.setText("Log out");
			
			userNameTextField.setText(userNameTextField.getText());
			userNameTextField.setEditable(false);
						
			pwdTextField.setEditable(false);
			
			regButton.setVisible(false);
			return true;
		}
		else{
			new ErrorMessageDialog("Incorrect username and/or password");
			return false;
		}
	}

	// Proof
	// Precondition: The user is signed in.
	// No matter the precondition, there will be no user logged in
	// at the end of the function.
	// Postcondition: The user is signed out.
	// There are no loops and all functions called terminate.
	// Therefore, the function must terminate.
	public void signOut() {
		dL.logOff();
		logButton.setText("Sign in");
		jLabel1.setText("Logon");
		
		userNameTextField.setEditable(true);
				
		pwdTextField.setText("");
		pwdTextField.setEditable(true);
		
		regButton.setVisible(true);
	}

	// Proof
	// There are no pre and post conditions. Function
	// just returns a global variable.
	// There are no loops and only one function is called.
	// Therefore, the function must terminate.
	public JButton getJButton()	{
		return logButton;
	}
	
	// Proof
	// There are no pre and post conditions. Function
	// just returns a global variable.
	// There are no loops and only one function is called.
	// Therefore, the function must terminate.	
	public JButton getRButton() {
		return regButton;
	}


}